#! /bin/sh

### BEGIN INIT INFO
# Provides: [[[linuxPackage.name]]]
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop [[[linuxPackage.longName]]]
### END INIT INFO 

# Shell script to start/stop [[[linuxPackage.longName]]]
# chkconfig: 2345 91 35
# description: This script starts and stops [[[linuxPackage.longName]]]
#

RUN_AS_USER=pentaho

APP_LONG_NAME="[[[linuxPackage.longName]]]"

PIDDIR="/var/run/pentaho"

# Process ID
PIDFILE="$PIDDIR/[[[linuxPackage.name]]].pid"
pid=""

# Resolve the location of the 'ps' command
PSEXE="/usr/bin/ps"
if [ ! -x "$PSEXE" ]
then
    PSEXE="/bin/ps"
    if [ ! -x "$PSEXE" ]
    then
        echo "Unable to locate 'ps'."
        echo "Please report this message along with the location of the command on your system."
        exit 1
    fi
fi

getpid() {
    if [ -f "$PIDFILE" ]
    then
        if [ -r "$PIDFILE" ]
        then
            pid=`cat "$PIDFILE"`
            if [ "X$pid" != "X" ]
            then
                # It is possible that 'a' process with the pid exists but that it is not the
                #  correct process.  This can happen in a number of cases, but the most
                #  common is during system startup after an unclean shutdown.
                # The ps statement below looks for the specific command running as
                #  the pid.  If it is not found then the pid file is considered to be stale.
                pidtest=`$PSEXE -p $pid -o args | grep "[[[linuxPackage.initd.processName]]]" | tail -1`
                if [ "X$pidtest" = "X" ]
                then
                    # This is a stale pid file.
                    su -m -c "rm -f \"$PIDFILE\"" $RUN_AS_USER
                    echo "Removed stale pid file: $PIDFILE"
                    pid=""
                fi
            fi
        else
            echo "Cannot read $PIDFILE."
            exit 1
        fi
    fi
}

testpid() {
    pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
    if [ "X$pid" = "X" ]
    then
        # Process is gone so remove the pid file.
        rm -f "$PIDFILE"
        pid=""
    fi
}

testpiddir()
{
  # Be sure that the PIDDIR exists
    if [ ! -d $PIDDIR ]
  then
    # If it doesn't exist, we create it
      mkdir -p $PIDDIR
      chown pentaho:pentaho $PIDDIR
  fi
}

usage() {
    echo "Usage: $0 <command>"
    echo "  Available Commands: "
    echo "      start       - Start it in the background"
    echo "      stop        - Stop it"
    echo "      restart     - Restart if it is already running,"
    echo "                    or start if it wasn't already running"
    echo "      status      - Display the status (running or stopped)"
}

start() {
    echo "Starting $APP_LONG_NAME..."
    getpid
     if [ "X$pid" = "X" ]
     then
        su -m -c "/opt/pentaho/[[[linuxPackage.name]]]/[[[linuxPackage.initd.startCommandDelegatee]]]" $RUN_AS_USER
     else
         echo "$APP_LONG_NAME is already running."
         exit 1
     fi


}
 
stopit() {
    echo "Stopping $APP_LONG_NAME..."
    getpid
    if [ "X$pid" = "X" ]
    then
        echo "$APP_LONG_NAME was not running."
    else
        # Running so try to stop it.
        su -m -c "/opt/pentaho/[[[linuxPackage.name]]]/[[[linuxPackage.initd.stopCommandDelegatee]]]" $RUN_AS_USER
        
        if [ $? -ne 0 ]
        then
            # An explanation for the failure should have been given
            echo "Unable to stop $APP_LONG_NAME."
            exit 1
        fi

        #  Loop until it stops.
        savepid=$pid
        CNT=0
        TOTCNT=0
        while [ "X$pid" != "X" ]
        do
            # Show a waiting message every 5 seconds.
            if [ "$CNT" -lt "5" ]
            then
                CNT=`expr $CNT + 1`
            else
                echo "Waiting for $APP_LONG_NAME to exit..."
                CNT=0
            fi
            TOTCNT=`expr $TOTCNT + 1`

            sleep 1

            testpid
        done

        pid=$savepid
        testpid
        if [ "X$pid" != "X" ]
        then
            echo "Failed to stop $APP_LONG_NAME."
            exit 1
        else
            echo "Stopped $APP_LONG_NAME."
        fi
    fi
}

status() {
    getpid
    if [ "X$pid" = "X" ]
    then
        echo "$APP_LONG_NAME is not running."
        exit 1
    else
        echo "$APP_LONG_NAME is running ($pid)."
        exit 0
    fi
}

case "$1" in

    'start')
        testpiddir
        start
        ;;

    'stop')
        stopit
        ;;

    'restart')
        stopit
        start
        ;;

    'status')
        status
        ;;

    *)
        echo "Usage: $0 { start | stop | restart | status }"
        exit 1
        ;;
esac

exit 0
